<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>绑定class样式</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
        .basic {
            width: 200px;
            height: 200px;
            font-size: 50px;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 200px;
            margin: 10px;
            transition: all 0.3s;
        }

        .happy {
            background-color: yellow;
            color: black;
        }

        .sad {
            background-color: blue;
            color: white;
        }

        .active {
            background-color: red;
            color: white;
            font-weight: bold;
        }

        .normal {
            background-color: green;
            color: white;
        }
    </style>
</head>
<body>
    <div id = "app">
        <div class="basic" :class="mood" @click="changeMood">
            {{age}}
        </div> <hr>
        <div class="basic" :class="mood1" @click="changeMood1">
            {{name}}
        </div> <hr>
        <div class="basic" :class="classObj" @click="changeMood2">
            {{sex}}
        </div>

    </div>
</body>
    <script>
        Vue.config.productionTip = false;
        new Vue({
            el: '#app',
            data: {
                name:"武汉",
                age:18,
                sex:"男",
                classArr:['happy', 'sad', 'active', 'normal'],
                mood: "",
                mood1:"",
                classObj:{
                    happy: false,
                    sad: false,
                    active: false,
                    normal: false
                }
            },
            methods: {
                // 绑定class样式的第一种写法。适用于：样式的类名不确定，需要动态指定
                changeMood(){
                    this.mood = "happy";
                },
                // 绑定class样式的第二种写法 数组写法。适用于：绑定样式的个数不确定,名字也不确定
                changeMood1(){
                    const randomIndex = Math.floor(Math.random() * this.classArr.length);
                    this.mood1 = this.classArr[randomIndex];
                },
                // 绑定class样式的第三种写法 对象写法。适用于：绑定的样式的个数确定，名字也确定，但要动态决定用不用
                changeMood2(){
                    this.classObj = {
                        happy: true,
                        sad: false,
                        active: false,
                        normal: false
                    }
                },

            }
        })
    </script>
</html>